//
//  MenuDetailViewController.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "MenuDetailViewController.h"

@interface MenuDetailViewController ()

@end

@implementation MenuDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.name;
    
    self.navigationController.navigationBar.translucent = NO;
    
    // 设置标题栏右侧的按钮(用于收藏与分享)
    UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect.png"];
    
    UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBarButton.backgroundColor = [UIColor clearColor];
    rightBarButton.frame = CGRectMake(0, 0, 25, 25);
    [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
    [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
    
    // 标记为未收藏
    self.collect = NO;
    
    
    // 打开数据库
    [[FoodDataBase shareMenuDataBaseHandle] openMenuDB];
    // 查询数据库中的信息
    for (MenuDetail *menuDetail in [[FoodDataBase shareMenuDataBaseHandle] selectMenu]) {
        
        // 判断当前收藏的食疗菜谱是否已经存在数据库中
        // 如果数据库中存在该菜谱, 显示已经收藏
        if ([menuDetail.name isEqualToString:self.name]) {
            // 修改标题栏右侧的按钮
            UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect2.png"];
            
            UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
            rightBarButton.backgroundColor = [UIColor clearColor];
            rightBarButton.frame = CGRectMake(0, 0, 25, 25);
            [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
            [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
            
            // 标记为已经收藏
            self.collect = YES;
        }
    }
    
    
    self.menuDetailScorll = [[MenuDetailScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64)];
    
    [self.view addSubview:self.menuDetailScorll];
    
    //初始化
    //接收制作步骤的数组,字典
    self.myArray = [NSMutableArray array];
    self.myDic = [NSMutableDictionary dictionary];
    
    //接收制作材料的数组,字典
    self.arr = [NSMutableArray array];
    self.dic = [NSMutableDictionary dictionary];
    
    
    //拼接制作步骤的字符串
    NSString *str1 = @"http://42.121.13.106:8080/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?&type=2&phonetype=0&is_traditional=0&";
    NSString *str2 = [NSString stringWithFormat:@"vegetable_id=%@",self.vegetable_id];
    NSString *str = [str1 stringByAppendingString:str2];
    [GetAFN downLoadBlock:str block:^(id result) {
        
        self.myDic = result;
        NSMutableArray *array = [NSMutableArray arrayWithArray:[self.myDic objectForKey:@"data"]];
        
        for (NSDictionary *dic in array) {
            MenuDetail *menuDetail = [[MenuDetail alloc] init];
            //kvc赋值
            [menuDetail setValuesForKeysWithDictionary:dic];
            //数据放入数组中
            [self.myArray addObject:menuDetail];
            
            [self.menuDetailScorll createSub:self.myArray menuDetail:menuDetail];
            
        }
        
        
        //由于是block,所以要在此处初始化,给model传数组,和model对象
        
        
        
        
    }];
    
    //调用getMenuDetail,请求制作材料的数据
    [self getMenuDetail];
    
}

//自定义方法,请求制作材料的数据
- (void)getMenuDetail
{
    //拼接制作材料的字符串
    NSString *str1 = @"http://42.121.13.106:8080/HandheldKitchen/api/vegetable/tblvegetable!getTblVegetables.do?&phonetype=2&user_id=101713&is_traditional=0&";
    NSString *str2 = [NSString stringWithFormat:@"vegetable_id=%@", self.vegetable_id];
    NSString *urlStr = [str1 stringByAppendingString:str2];
    
    
    
    
    [GetAFN downLoadBlock:urlStr block:^(id result) {
        self.dic = result;
        NSMutableArray *array = [NSMutableArray arrayWithArray:[self.dic objectForKey:@"data"]];
        
        for (NSDictionary *dictionary in array) {
            
            MenuDetail *menuDetail = [[MenuDetail alloc] init];
            
            
            [menuDetail setValuesForKeysWithDictionary:dictionary];
            //调用model页面的自定义赋值方法
            [self.menuDetailScorll setMenuDetail:menuDetail];
            [self.arr addObject:menuDetail];
            
        }
        
    }];
    
    
}

- (void)rightBarButtonAction:(UIBarButtonItem *)button

{
    if (self.collect == YES) {
        // 弹出提示框
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"分享", @"取消收藏", nil];
        [actionSheet showInView:self.view];
    }else{
        // 弹出提示框
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"分享", @"收藏", nil];
        [actionSheet showInView:self.view];
    }
}


//点击actionsheet的方法(协议)
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSInteger *temp = 0;
    
    if (buttonIndex == 0) {
        NSLog(@"分享");
    }
    else if (buttonIndex == 1){
        NSLog(@"收藏");
        
        MenuDetail *menuDetail = [[MenuDetail alloc] init];
        menuDetail.name = self.name;
        menuDetail.vegetable_id = self.vegetable_id;
        
        // 打开数据库
        [[FoodDataBase shareMenuDataBaseHandle]openMenuDB];
        // 创建数据库表
        [[FoodDataBase shareMenuDataBaseHandle]createMenuTable];
        // 查询数据库
        NSMutableArray *array = [[FoodDataBase shareMenuDataBaseHandle] selectMenu];
        for (MenuDetail *menuDetail in array) {
            
            
            if ([menuDetail.name isEqualToString:self.name]) {
                // 标记是否收藏过
                temp++;
                
                [[FoodDataBase shareMenuDataBaseHandle] deleteMenu:menuDetail.name];
                
                // 修改标题栏右侧的按钮
                UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect.png"];
                
                UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
                rightBarButton.backgroundColor = [UIColor clearColor];
                rightBarButton.frame = CGRectMake(0, 0, 25, 25);
                [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
                [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
                
                
                [self showAlert:@"取消收藏!"];
                
                // 修改ActionSheet的点击标记
                self.collect = NO;
                
            }
        }
        {
            if (temp == 0) {
                [[FoodDataBase shareMenuDataBaseHandle]insertMenu:menuDetail];
                
                // 修改标题栏右侧的按钮
                UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect2.png"];
                
                UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
                rightBarButton.backgroundColor = [UIColor clearColor];
                rightBarButton.frame = CGRectMake(0, 0, 25, 25);
                [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
                [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
                
                [self showAlert:@"收藏成功!"];
                
                // 修改ActionSheet的点击标记
                self.collect = YES;
            }
        }
    }
}
// 设置弹出提示框, 自动消失的方法
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}

- (void)showAlert:(NSString *) message{//时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    // 定时器
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:promptAlert repeats:YES];
    [promptAlert show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
